package View.Repair;

import Service.DormService;
import Service.RepairService;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.RepairServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import View.Index.Index_user;
import domain.Dorm;
import domain.Repair;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class RepairPage extends JFrame {


    public RepairPage(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("报修提交");
        this.setSize(506,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("宿舍号:");
        label1.setBounds(111,21,65,30);
        JLabel label2= new JLabel();
        label2.setText("故障:");
        label2.setBounds(111,71,65,30);
        JLabel label3= new JLabel();
        label3.setText("描述:");
        label3.setBounds(110,122,65,30);

        //------------------JTextField--------------------
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(190,22,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(191,73,120,30);

        JTextArea text = new JTextArea();
        text.setText("");
        text.setLineWrap(true);
        text.setBounds(192,125,200,110);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("提交");
        button1.setBounds(107,249,95,27);
        JButton button2= new JButton();
        button2.setText("返回");
        button2.setBounds(249,250,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(comboBox2);
        this.add(field2);
        this.add(text);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Repair repair = new Repair();
                repair.setDormnum((String) comboBox2.getSelectedItem());
                repair.setProblem(field2.getText());
                repair.setDescription(text.getText());
                repair.setIsfinish("false");

                RepairService service = new RepairServiceImpl();
                service.addRepair(repair);

                systemMsg msg = new systemMsg();
                msg.setUsername("管理员");
                msg.setTitle("宿舍报修申请");
                msg.setNeirong("宿舍:"+repair.getDormnum()+"申请报修,故障物品:"+repair.getProblem()+"，详细描述:"+repair.getDescription());
                msg.setSendTime(new Date());
                msg.setIsread("false");
                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"提交成功,等待管理员审批后,维修人员将进行维修");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_user(user).setVisible(true);
            }
        });
    }
}
